package com.sys.user.controller;

import com.app.utils.MD5;
import com.app.utils.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.aspect.Log;
import com.sys.menu.service.SysMenuService;
import com.sys.role.entity.SysRole;
import com.sys.role.service.SysRoleService;
import com.sys.user.entity.SysUser;
import com.sys.user.service.SysUserService;
import com.sys.user.view.SysUserView;
import com.sys.userrole.entity.SysUserRole;
import com.sys.userrole.service.SysUserRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author djianfeng
 * @since 2018-07-24
 */
@Controller
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    SysUserService userService;
    @Autowired
    SysUserRoleService userRoleService;
    @Autowired
    SysMenuService menuService;
    @Autowired
    SysRoleService roleService;

    @Log("查询用户列表")
    @RequiresPermissions("UserList")
    @RequestMapping(value = "/list.do", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(ServletRequest request, ServletResponse response, Page<SysUserView> page, SysUserView view) {
        Map<String, Object> map = new HashMap<>();
        List<SysUserView> list = userService.getSysUserViews(page, view);
        map.put("records", list);
        map.put("total", page.getTotal());
        return map;
    }

    @Log("进入用户管理")
    @RequiresPermissions("User")
    @RequestMapping("/golist.do")
    public String tolist() {
        return "system/user/list";
    }

    @Log("查看用户")
    @RequiresPermissions("EditUser")
    @RequestMapping("/goEdit.do")
    public ModelAndView goEdit(SysUser entity) {
        Map map = new HashMap();
        if (entity.getId() != null) {
            entity = this.userService.selectById(entity.getId());
        }
        map.put("entity", entity);
        return new ModelAndView("system/user/edit", map);
    }

    @Log("获取用户角色")
    @RequiresPermissions("UpdateUserRole")
    @RequestMapping("/goUserRole.do")
    public ModelAndView goUserRole(SysUser entity) {
        Map map = new HashMap();
        map.put("user_id", entity.getId());
        List<SysUserRole> list = (List)userRoleService.selectByMap(map);
        if (list != null && !list.isEmpty()) {
            Long[] roleIds = new Long[list.size()];
            for (int i = 0; i < list.size(); i++) {
                roleIds[i] = list.get(i).getRoleId();
            }
            map.put("roleIds", roleIds);
        }
        List<SysRole> roles = (List)roleService.selectByMap(new HashMap<>());
        map.put("roles", roles);
        map.put("entity", entity);
        return new ModelAndView("system/user/role", map);
    }

    @Log("保存用户")
    @RequiresPermissions("EditUser")
    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    @ResponseBody
    public Result save(SysUser entity) {
        if (entity.getId() == null) {
            entity.setCreateTime(System.currentTimeMillis());
            entity.setUserState("1");
            entity.setUserType("1");
            entity.setPassword(MD5.toMD5("123456"));
        }
        boolean n = this.userService.insertOrUpdate(entity);
        return Result.returnResult(n);
    }

    @Log("修改用户角色")
    @RequiresPermissions("UpdateUserRole")
    @RequestMapping(value = "/saveUserRole.do", method = RequestMethod.POST)
    @ResponseBody
    public Result saveUserRole(SysUserView view) {
        Map map = new HashMap();
        map.put("user_id", view.getId());
        boolean n = this.userRoleService.deleteByMap(map);
        if (view.getRoleIds() != null && view.getRoleIds().length > 0) {
            for (int i = 0; i < view.getRoleIds().length; i++) {
                SysUserRole userRole = new SysUserRole();
                userRole.setRoleId(view.getRoleIds()[i]);
                userRole.setUserId(view.getId());
                this.userRoleService.insert(userRole);
            }
        }
        return Result.returnResult(n);
    }

    @Log("删除用户")
    @RequiresPermissions("DelUser")
    @RequestMapping(value = "/del.do", method = RequestMethod.POST)
    @ResponseBody
    public Result del(SysUser entity) {
        boolean n = this.userService.deleteById(entity.getId());
        return Result.returnResult(n);
    }

    @Log("修改用户")
    @RequiresPermissions("UpdateUser")
    @RequestMapping(value = "/update.do", method = RequestMethod.POST)
    @ResponseBody
    public Result update(SysUser entity) {
        boolean n = this.userService.updateById(entity);
        return Result.returnResult(n);
    }


}
